!preparing data for calculations
subroutine init3d(a,i,i_vertex2D,i_nVer2D,i_nInCat2D,i_vertex3D,i_nVer3D,&
    i_nInCat3D,i_igT,i_jgT,i_ggT,i_ig,i_jg,i_ggl,i_ggu,i_di,i_f,i_l1,i_v1,i_v2,i_v3,i_v4,i_v5,&
    i_ggl2,i_ggu2,i_di2,i_vnRZ,i_vnXYZ,i_igT3D,i_jgT3D,i_ggT3D,i_l13d,i_va,i_ig3D,i_jg3D,&
    i_igEarth,i_jgEarth,i_a,i_nodesforedges,i_a0,i_params)
    dimension a(100000000),i(100000000)
    real*8 a,eps2d,eps3d,radiusRED
    integer inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,rz_dat,tsize_dat,&
    gg_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,va_dat,edges_dat,a_0000,sig3d
    integer elemsCount2D,edgesTermCount3D,edgesNotTermCount3D,edgesCount3D,elemsCount3D,elementsEarth
    integer typeTask
    common /files/inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,nver_dat,nvkat_dat,&
    inf2tr_dat,nvkat2d_dat,rz_dat,nvtr_dat,tsize_dat,ig_dat,jg_dat,gg_dat,l1_dat,&
    ig3d_dat,jg3d_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,&
    l13d_dat,va_dat,nodesforedges_dat,edges_dat,a_0000,sig3d
    common /dim2D/nodesCount2D,elemsCount2D,nodesBoundCount2D,nodesTermCount2D,m2D,mTerm2d,nodesNotTermCount2D
    common /dim3D/nodesCount3D,elemsCount3D,nodesBoundCount3D,edgesTermCount3D,edgesNotTermCount3D,&
    edgesCount3D,nodesTermCount3D,m3D,mTerm3d,nodesNotTermCount3D
    common /commonInfo/ eps2d,eps3d,radiusRED,maxiter,typeTask,nt,nMaterials
    common /commonIndex/ indexI,indexA
    common /dimEarth/nodesNotTermEarth,mEarth

    !opening files
    call open_files()

    !input 2D data
101 format(' ISLAU=',i8,' INDKU1=',i8,' INDFPO=',i8)
102 format('KUZLOV=',i8,'    KPAR=',i8,'    KT1=',i8,'    KT2=',i8,'    KT3=',i8)
103 format('KISRS1=',i8,' KISRS2=',i8,' KISRS3=',i8,'   KBRS=',i8)
104 format('KREB4 =',i8,' KTSIM =',i8)
    read(inf2tr_dat,101)k,k,k
    read(inf2tr_dat,102) nodesCount2D,elemsCount2D,nodesBoundCount2D,k1,k2
    read(inf2tr_dat,103) k,k,k,k
    read(inf2tr_dat,104) kreb4,k
    read(tsize_dat,*)nodesTermCount2D
    nodesNotTermCount2D = nodesCount2D-nodesTermCount2D

    i_vertex2D = indexA
    i_ggT      = i_vertex2D + 2*nodesCount2D
    i_nVer2D   = indexI
    i_nInCat2D = i_nVer2D+6*elemsCount2D
    i_l1       = i_nInCat2D+elemsCount2D
    i_igT      = i_l1+nodesBoundCount2D
    i_jgT      = i_igT+nodesTermCount2D+1
    call input2D(a(i_vertex2D),i(i_nVer2D),i(i_nInCat2D),i(i_igT),i(i_jgT),a(i_ggT),i(i_l1))
    i_listbeg  = i_jgT+mTerm2d
    i_ig       = i_listbeg+nodesNotTermCount2D
    i_jg       = i_ig+nodesNotTermCount2D+1
    call portrait(i(i_ig),i(i_jg),nodesCount2D,elemsCount2D,i(i_nVer2D),m2d,i(i_listbeg),6,4,i(i_igT),i(i_jgT),&
    mTerm2d,nodesNotTermCount2D)

    !input 3D data
105 format(' ISLAU=',i8,' INDKU1=',i8,' INDFPO=',i8)
106 format('KUZLOV=',i8,'    KPAR=',i8,'    KT1=',i8)
    read(inftry_dat,101)k,k,k
    read(inftry_dat,102) nodesCount3D,elemsCount3D,nodesBoundCount3D
    read(t_size_dat,*)edgesTermCount3D
    read(t_size_dat,*)edgesNotTermCount3D
    read(t_size_dat,*)edgesCount3D
    read(tsize3d_dat,*)nodesTermCount3D
    nodesNotTermCount3D = nodesCount3D-nodesTermCount3D
    i_params   = i_ggT+mTerm2d
    call inputParams_task3d(a(i_params),sig3d,nMaterials)
    i_vertex3D = i_params+2*nMaterials
    i_ggT3D = i_vertex3D + 3*nodesCount3D
    i_nVer3D = i_jg+m2d
    i_nInCat3D = i_nVer3D+14*elemsCount3D
    i_nodesforedges = i_nInCat3D+elemsCount3D
    i_l13d = i_nodesforedges+3*edgesCount3D
    i_igT3D =  i_l13d+nodesBoundCount3D
    i_jgT3D = i_igT3D+nodesTermCount3D+1
    call input3D(a(i_vertex3D),i(i_nVer3D),i(i_nInCat3D),i(i_igT3D),i(i_jgT3D),a(i_ggT3D),i(i_l13d),i(i_nodesforedges))
    i_listbeg = i_jgT3D+mTerm3d    
    i_ig3D = i_listbeg+nodesNotTermCount3D
    i_jg3D = i_ig3D+nodesNotTermCount3D+1
    call portrait(i(i_ig3D),i(i_jg3D),nodesCount3D,elemsCount3D,i(i_nVer3D),m3d,i(i_listbeg),14,8,i(i_igT3D),i(i_jgT3D),&
    mTerm3d,nodesNotTermCount3D)
    i_igEarth=i_jg3D+m3d
    i_jgEarth=i_igEarth+nodesNotTermEarth+1
    call getPortraitForEarth(i(i_ig3D),i(i_jg3D),i(i_igEarth),i(i_jgEarth))
    i_ggl = i_ggT3D+mTerm3d
    i_ggu = i_ggl+m3d
    i_di = i_ggu+m3d
    i_f = i_di+nodesNotTermCount3D
    i_v1 = i_f+nodesNotTermCount3D*3
    i_v2 = i_v1+nodesNotTermCount3D
    i_v3 = i_v2+nodesNotTermCount3D
    i_v4 = i_v3+nodesNotTermCount3D
    i_v5 = i_v4+nodesNotTermCount3D
    i_ggl2 = i_v5+nodesNotTermCount3D
    i_ggu2 = i_ggl2+m3d
    i_di2 = i_ggu2+m3d
    i_vnXYZ=i_di2+nodesNotTermCount3D
    i_va=i_vnXYZ+nodesCount3D
    i_vnRZ=i_va
    i_a0=i_vnXYZ
    i_a=i_a0+edgesNotTermCount3D

1   format(A,i8,' nElement =',i8)
    write(*,1)'    2d:            nVertex =',nodesCount2D,elemsCount2D
    write(*,1)'    3d:            nVertex =',nodesCount3D,elemsCount3D
!    write(log_txt,1)'2d:            nVertex =',nodesCount2D,elemsCount2D
!    write(log_txt,1)'3d:            nVertex =',nodesCount3D,elemsCount3D
end
!*************************************************************************
!opening files
subroutine open_files()
    common /files/inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,nver_dat,nvkat_dat,&
    inf2tr_dat,nvkat2d_dat,rz_dat,nvtr_dat,tsize_dat,ig_dat,jg_dat,gg_dat,l1_dat,&
    ig3d_dat,jg3d_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,&
    l13d_dat,va_dat,nodesforedges_dat,edges_dat,a_0000,sig3d
    integer inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,rz_dat,tsize_dat,&
    gg_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,va_dat,edges_dat,&
    a_0000,sig3d

   !mesh2D 
    inf2tr_dat  = 201
    nvkat2d_dat = 202
    rz_dat      = 203
    nvtr_dat    = 204
    tsize_dat   = 205
    ig_dat      = 206
    jg_dat      = 207
    gg_dat      = 208
    l1_dat      = 209
    open(inf2tr_dat,file='.\calculations\mesh2d\inf2tr.dat',mode='read')
    open(nvkat2d_dat,file='.\calculations\mesh2d\nvkat2d.dat',access='direct',recl=4,mode='read')
    open(rz_dat,file='.\calculations\mesh2d\rz.dat',access='direct',recl=16,mode='read')
    open(nvtr_dat,file='.\calculations\mesh2d\nvtr.dat',access='direct',recl=24,mode='read')
    open(tsize_dat,file='.\calculations\mesh2d\tsize.dat',mode='read')
    open(ig_dat,file='.\calculations\mesh2d\ig.dat',access='direct',recl=4,mode='read')
    open(jg_dat,file='.\calculations\mesh2d\jg.dat',access='direct',recl=4,mode='read')
    open(gg_dat,file='.\calculations\mesh2d\gg.dat',access='direct',recl=8,mode='read')
    open(l1_dat,file='.\calculations\mesh2d\l1.dat',access='direct',recl=4,mode='read')

    !mesh3D
    inftry_dat  = 101
    t_size_dat  = 102
    tsize3d_dat = 103
    xyz_dat     = 104
    nver_dat    = 105
    nvkat_dat   = 106
    ig3d_dat    = 107
    jg3d_dat    = 108
    gg3d_dat    = 109
    l13d_dat    = 110
    nodesforedges_dat = 111
    edges_dat   = 112
    sig3d       = 113
    open(inftry_dat,file='.\calculations\mesh3d\inftry.dat',mode='read')
    open(t_size_dat,file='.\calculations\mesh3d\t_size.dat',mode='read')
    open(tsize3d_dat,file='.\calculations\mesh3d\tsize3d.dat',mode='read')
    open(xyz_dat,file='.\calculations\mesh3d\xyz.dat',access='direct',recl=24,mode='read')
    open(nver_dat,file='.\calculations\mesh3d\nver.dat',access='direct',recl=56,mode='read')
    open(nvkat_dat,file='.\calculations\mesh3d\nvkat.dat',access='direct',recl=4,mode='read')
    open(ig3d_dat,file='.\calculations\mesh3d\ig3d.dat',access='direct',recl=4,mode='read')
    open(jg3d_dat,file='.\calculations\mesh3d\jg3d.dat',access='direct',recl=4,mode='read')
    open(gg3d_dat,file='.\calculations\mesh3d\gg3d.dat',access='direct',recl=8,mode='read')
    open(l13d_dat,file='.\calculations\mesh3d\l13d.dat',access='direct',recl=4,mode='read')
    open(nodesforedges_dat,file='.\calculations\mesh3dv\nodesforedges.dat',access='direct',recl=8,mode='read')
    open(edges_dat,file='.\calculations\mesh3dv\edges.dat',access='direct',recl=100,mode='read')
    open(sig3d,file='.\calculations\sig3d',mode='read')

    !solutions
    vn2d_dat = 401
    vn3d_dat = 402
    va_dat   = 403
    ax_dat   = 404
    ay_dat   = 405
    az_dat   = 406
    a_0000   = 407
    open(vn2d_dat,file='.\calculations\vn2d.dat',access='direct',recl=8,status='replace',mode='write')
    open(vn3d_dat,file='.\calculations\vn3d.dat',access='direct',recl=8,status='replace',mode='write')
    open(va_dat,file='.\calculations\va.dat',access='direct',recl=8,status='replace',mode='write')
    open(ax_dat,file='.\calculations\ax.dat',access='direct',recl=8,status='replace',mode='write')
    open(ay_dat,file='.\calculations\ay.dat',access='direct',recl=8,status='replace',mode='write')
    open(az_dat,file='.\calculations\az.dat',access='direct',recl=8,status='replace',mode='write')
    open(a_0000,file='.\calculations\A\a.0000',access='direct',recl=8,status='replace',mode='write')
end
!*************************************************************************
subroutine input2D(vertex,nVer,nInCat2D,igT,jgT,ggT,l1)
    common /files/inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,nver_dat,nvkat_dat,&
    inf2tr_dat,nvkat2d_dat,rz_dat,nvtr_dat,tsize_dat,ig_dat,jg_dat,gg_dat,l1_dat,&
    ig3d_dat,jg3d_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,&
    l13d_dat,va_dat,nodesforedges_dat,edges_dat,a_0000,sig3d
    common /dim2D/nodesCount2D,elemsCount2D,nodesBoundCount2D,nodesTermCount2D,m2D,mTerm2d,nodesNotTermCount2D
    integer inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,rz_dat,tsize_dat,typeTask_txt,&
    gg_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,va_dat,edges_dat,a_0000,sig3d
    integer elemsCount2D,edgesTermCount3D,edgesNotTermCount3D,edgesCount3D,elemsCount3D
    real*8 vertex,ggT
    dimension vertex(2,nodesCount2D),nVer(6,elemsCount2D),nInCat2D(elemsCount2D),&
    igT(nodesTermCount2D+1),jgT(*),ggT(*),l1(nodesBoundCount2D)

    do j=1,nodesCount2D
        read(rz_dat,rec=j)vertex(1,j),vertex(2,j)
    enddo
    
    n=1
    do j=1,elemsCount2D
        read(nvkat2D_dat,rec=j)nInCat2D(j)
        read(nvtr_dat,rec=j)nVer(3,j),nVer(4,j),nVer(1,j),nVer(2,j),nVer(5,j),nVer(6,j)
    enddo

    do j=1,nodesTermCount2D+1
        read(ig_dat,rec=j)igT(j)
    enddo
    mTerm2d=igT(nodesTermCount2D+1)-1

    do j=1,mTerm2d
        read(jg_dat,rec=j)jgT(j)
        read(gg_dat,rec=j)ggT(j)
    enddo

    do j=1,nodesBoundCount2D
        read(l1_dat,rec=j)l1(j)
    enddo
end
!*************************************************************************
subroutine input3D(vertex,nVer,nInCat3D,igT3D,jgT3D,ggT3D,l13d,nodesforedges)
    common /files/inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,nver_dat,nvkat_dat,&
    inf2tr_dat,nvkat2d_dat,rz_dat,nvtr_dat,tsize_dat,ig_dat,jg_dat,gg_dat,l1_dat,&
    ig3d_dat,jg3d_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,&
    l13d_dat,va_dat,nodesforedges_dat,edges_dat,a_0000,sig3d
    common /dim3D/nodesCount3D,elemsCount3D,nodesBoundCount3D,edgesTermCount3D,edgesNotTermCount3D,&
    edgesCount3D,nodesTermCount3D,m3D,mTerm3d,nodesNotTermCount3D
    common /dimEarth/nodesNotTermEarth,mEarth
    integer inftry_dat,t_size_dat,tsize3d_dat,xyz_dat,rz_dat,tsize_dat,typeTask_txt,&
    gg_dat,gg3d_dat,vn2d_dat,vn3d_dat,ax_dat,ay_dat,az_dat,va_dat,edges_dat,a_0000,sig3d
    integer edgesTermCount3D,edgesNotTermCount3D,edgesCount3D,elemsCount3D,edges
    real*8 vertex,ggT3D
    dimension vertex(3,nodesCount3D),nVer(14,elemsCount3D),nInCat3D(elemsCount3D),&
    igT3D(nodesTermCount3D+1),jgT3D(*),ggT3D(*),l13d(nodesBoundCount3D),nodesforedges(3,edgesCount3D),&
    edges(12)

    nodesNotTermEarth=1
    do j=1,nodesCount3D
        read(xyz_dat,rec=j)vertex(1,j),vertex(2,j),vertex(3,j)
        if(j.le.nodesNotTermCount3D.and.vertex(3,j).le.0.d0.and.nodesNotTermEarth.lt.j) nodesNotTermEarth=j
    enddo

    do j=1,elemsCount3D
        read(nver_dat,rec=j)(nVer(k,j),k=1,14)
        read(nvkat_dat,rec=j)nInCat3D(j)
    enddo

    do j=1,nodesTermCount3D+1
        read(ig3d_dat,rec=j)igT3D(j)
    enddo
    mTerm3d=igT3D(nodesTermCount3D+1)-1

    do j=1,igT3D(nodesTermCount3D+1)-1
        read(jg3d_dat,rec=j)jgT3D(j)
        read(gg3d_dat,rec=j)ggT3D(j)
    enddo

    do j=1,nodesBoundCount3D
        read(l13d_dat,rec=j)l13d(j)
    enddo

    do j=1,edgesCount3D
        read(nodesforedges_dat,rec=j) nodesforedges(1,j),nodesforedges(2,j)
    enddo

    do j=1,elemsCount3D
        read(edges_dat,rec=j) (edges(k),k=1,12),l,l,l,l,l,l,l,l,l,l,l,l,l
        do k=1,12
            nodesforedges(3,edges(k))=j
        enddo
    enddo    
end
!*************************************************************************
subroutine getPortraitForEarth(ig,jg,igEarth,jgEarth)
    common /dim3D/nodesCount3D,elemsCount3D,nodesBoundCount3D,edgesTermCount3D,edgesNotTermCount3D,&
    edgesCount3D,nodesTermCount3D,m3D,mTerm3d,nodesNotTermCount3D
    common /dimEarth/nodesNotTermEarth,mEarth
    integer edgesTermCount3D,edgesNotTermCount3D,edgesCount3D,elemsCount3D,elementsEarth
    dimension ig(nodesNotTermCount3D+1),jg(m3D),igEarth(nodesNotTermEarth+1),jgEarth(*)

    k=1
    igEarth(1)=1
    do i=1,nodesNotTermEarth
        j=ig(i)
        do while(j<=ig(i+1)-1.and.jg(j)<=nodesNotTermEarth)
            jgEarth(k)=jg(j)
            j=j+1
            k=k+1
        enddo
        igEarth(i+1)=igEarth(i)+(j-ig(i))
    enddo
    mEarth=igEarth(nodesNotTermEarth+1)-1
end
!*************************************************************************
!*************************************************************************
!*************************************************************************